package com.cc.demo.controller;

import com.cc.demo.dto.TransactionMessage;
import com.cc.demo.entity.TransactionEventMessage;
import com.cc.demo.service.TransactionEventMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/transaction")
@RequiredArgsConstructor
public class TransactionMessageController {

    private final TransactionEventMessageService transactionEventMessageService;

    /**
     * 暴露接口 通过该接口来存储预发送的消息
     *
     * @param transactionMessage
     * @return
     */
    //TODO 需要包装一个common的类型, 处理返回异常的情况
    //TODO 或者利用TransactionMessage 的result和note的来处理异常的情况
    @RequestMapping(value = "/prepare", method = RequestMethod.POST)
    public TransactionMessage<?> prepareMessage(@RequestBody TransactionMessage<?> transactionMessage) {
        TransactionEventMessage transactionEventMessage = transactionEventMessageService.convertMessageToTransactionEventMessage(transactionMessage);
        TransactionEventMessage messageWaitingConfirm = transactionEventMessageService.saveMessageWaitingConfirm(transactionEventMessage);
        return transactionEventMessageService.convertTransactionEventMessageToMessage(messageWaitingConfirm);
    }

}
